<?php

namespace App\Http\Controllers\admin\defaults;


use App\Helpers\commonFuc;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


class AdminController extends BaseController
{
    private $tpviewhead='admin.defaults.admin.';
    private $table='admin';
    function __construct(Request $thisRequest)
    {
        parent::__construct($thisRequest,$this->tpviewhead,$this->table);
    }
    function lists()
    {
        //条件输入
        $username=$this->thisRequest->get('username');
        $realname=$this->thisRequest->get('realname');
        $mobile=$this->thisRequest->get('mobile');
        //排序输入
        $sort =$this->thisRequest->get('sort');
        $limit = (int)$this->thisRequest->get('limit');
        $limit = $limit ? $limit : 15;
        $data = $this->thisModel;
        //关联表
        $data =$data->leftJoin('admin_group as a0','a0.id','=','admin.group_id')
            ->select('admin.id','admin.username','admin.password','admin.group_id','admin.s_id','admin.checkrules','admin.rules','admin.yyproduct','admin.sex','admin.status','admin.realname','admin.touxiang','admin.mobile','admin.nick','admin.weixin','admin.weixin_img','admin.qq','admin.email','admin.remark','admin.login_ip','admin.login_time','admin.create_time','admin.paw_time','admin.update_time','admin.updated_at','admin.created_at'
                ,'a0.title');
        //条件
        if($username){
            $data =$data->where('admin.username', 'like', '%'.$username.'%');
        }
        if($realname){
            $data =$data->where('admin.realname', 'like', '%'.$realname.'%');
        }
        if($mobile){
            $data =$data->where('admin.mobile', 'like', '%'.$mobile.'%');
        }
        //排序
        if($sort){
            $sort=(explode(' ',$sort));
            $data =$data->orderBy($sort[0],$sort[1]);
        }

        $data =$data ->paginate($limit);
        if($this->thisRequest->ajax()){

            return commonFuc::data_reture_json($data,200,'');
        }


        return view($this->tpviewhead . 'lists',['data'=>$data]);
    }

    function dele()
    {
        $id = $this->thisRequest->get('id');
        $ids = $this->thisRequest->get('ids');
        $ids = array_merge(explode(',', $id),explode(',', $ids));
        if(empty($ids) || in_array(1,$ids)){
            if($this->thisRequest->ajax()){
                return commonFuc::data_reture_json([], 400, 'id错误');
            }else{
                return '<script>alert("id错误");window.history.go(-1);</script>';
            }
        }

        $data = $this->thisModel->whereIn('id',$ids)->delete();
        if($this->thisRequest->ajax()){

            return commonFuc::data_reture_json([], 200, '删除成功');
        }else{
            return '<script>alert("删除成功");window.history.go(-1);location.reload();</script>';
        }
    }
    function add(){
        if( $this->isPostOrAjax){
            return $this->save();
        }
        $gorup = DB::table('admin_group')->get();
        return view($this->tpviewhead . 'add',['gorup'=>$gorup]);

    }
    function edit(){
        if( $this->isPostOrAjax){
            return $this->save();
        }
        $GLOBALS['isjson'] = false;
        $data = $this->info(true);
        $gorup = DB::table('admin_group')->get();

        return view($this->tpviewhead . 'edit',['data'=>$data,'gorup'=>$gorup]);

    }
    function save($input=[])
    {
        if(empty($input)){
            $id = $this->thisRequest->input('id');
            $input=($this->thisRequest->all());

        }else{
            $id = isset($input['id']) ? $input['id'] : 0;
        }
        $input = $this->checkTableField($input);
        if(empty($input)){
            if($this->thisRequest->ajax()){
                return commonFuc::data_reture_json([],400,'参数错误没有符合字段');
            }else{
                return '<script>alert("参数错误没有符合字段");window.history.go(-1);</script>';
            }
        }
        if($id>0){
            $data = $this->thisModel->where('id',$id)->update($input);
            if($this->thisRequest->ajax()){
                return commonFuc::data_reture_json($data,200,'修改成功');
            }else{
                return '<script>alert("修改成功");window.history.go(-1);location.reload();</script>';
            }
        }else{
            unset($input['id']);
            if(isset($input['password'])){
                $input['password'] = md5($input['password'].'Td');
            }
            $data = $this->thisModel->insertGetId($input);
            if($this->thisRequest->ajax()){
                return commonFuc::data_reture_json($data,200,'新增成功');
            }else{
                return '<script>alert("新增成功");window.history.go(-1);location.reload();</script>';
            }
        }
    }

    //密码重置
    function resetpasswords(){
        $id=$this->thisRequest->get('id');
        if($id<1){
            return commonFuc::data_reture_json('',400,'请选择要重置的用户');
        }
        $this->thisModel->where('id', '=', $id)->update([
            'password' => md5( 'a123456Td')
        ]);
        return commonFuc::data_reture_json('',200,'已为您成功重置为 a123456 密码！');
    }


    //修改密码
    function editmypasswords(){
        $loginadmin=session()->get('admin');
        if( $this->isPostOrAjax){
            $pas_old = $this->thisRequest->get('old_password');
            $pas_old = md5($pas_old . 'Td');
            $pas = $this->thisRequest->get('password');
            $pas2 = $this->thisRequest->get('password2');

            if($pas_old!=$loginadmin->password){
                return commonFuc::data_reture_json('',400,'旧密码不正确！');
            }
            if($pas!=$pas2){
                return commonFuc::data_reture_json('',400,'2次输入的密码不一致！');
            }
            $this->thisModel->where('id', '=', $loginadmin->id)->update([
                'password' => md5( $pas.'Td')
            ]);
            return commonFuc::data_reture_json('',200,'密码修改成功，请重新登录！');
        }


        return view($this->tpviewhead . 'editmypasswords');
    }
    //修改我的个人信息
    function editmyinfo(){
        if( $this->isPostOrAjax){
            $input=($this->thisRequest->all());
            if(isset($input['remark'])){ unset($input['remark']); }
            if(isset($input['status'])){ unset($input['status']); }
            if(isset($input['group_id'])){ unset($input['group_id']); }
            if(isset($input['username'])){ unset($input['username']); }
            if(isset($input['password'])){ unset($input['password']); }
            $loginadmin=session()->get('admin');
            $input['id'] = $loginadmin->id;
            //dd($this->userData);
            return $this->save($input);
        }
        $GLOBALS['isjson'] = false;
        $data = $this->info(true);
        $gorup = DB::table('admin_group')->get();

        return view($this->tpviewhead . 'editmyinfo',['data'=>$data,'gorup'=>$gorup]);

    }


}